<template>
  <div class="main-content-container">
    <!-- 物品-分类 -->
    <div ref="search" class="app-search">
      <el-row>
        <!-- <el-col :span="4"> -->
        <!-- <el-select v-model="value" placeholder="请选择" v-if="isShow" @change="hositalList">
          <el-option v-for="item in hospitalList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select> -->
        <!-- </el-col> -->
        <el-col :span="24" align="left" class="addBtnStyle">
          <el-button @click="handleAdd('增加分类',true)">
            +增加分类
          </el-button>
        </el-col>
      </el-row>
    </div>
    <div class="box-show marginTop" :style="'height:'+ (innerHeight + 80) + 'px'">
      <el-table v-loading="loading" :data="tableData" border :height="innerHeight" row-key="id" :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
        <el-table-column prop="no" label="编号" :show-overflow-tooltip="true" />
        <el-table-column prop="status" label="状态" :show-overflow-tooltip="true" />
        <el-table-column prop="dataName" label="物品名称" :show-overflow-tooltip="true" />
        <el-table-column prop="address" label="操作" :show-overflow-tooltip="true">
          <template slot-scope="{ row }">
            <el-button type="text" @click="handleAdd('增加子分类',true, row)">增加子分类</el-button>
            <el-button type="text" @click="handleAdd('编辑分类',true, row,true)">编辑</el-button>
            <el-button slot="reference" :loading="buttonLoading" type="text" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination :page-num="queryParams.page" :page-size="queryParams.pageSize" :on-change="handlePaginationChange" :total="total" />
    </div>
    <Dialog :title="title" button-text="保存" class="single-column" :loading="loading" :visible="visible" @confirm="submitForm" @close="handleAdd('',false,{})">
      <AddProduct v-if="visible" ref="addProduct" :row="currentRow" :is-edit.sync="isEdit" @success="handleSuccess" />
    </Dialog>
  </div>
</template>
<script>
import Dialog from '@/components/Dialog';
import { labelHeigh } from '@/utils/common.js';
import AddProduct from './addProduct';
export default {
  components: {
    Dialog,
    AddProduct,
  },
  data() {
    return {
      title: '增加物品分类',
      visible: false,
      reveal: false,
      total: 0,
      search: {},
      buttonLoading: false,
      queryParams: {
        page: 1,
        pageSize: this.$store.state.commonsettings.maxPageSize,
      },
      value: '',
      // tableColor,
      innerHeight: null,
      loading: false,
      tableData: [],
      currentRow: {},
      isEdit: false,
      hospitalList: [],
      isShow: true,
    };
  },
  mounted() {
    this.getList();
    // this.getHospitalList();
    this.innerHeight = labelHeigh(this.$refs.search);
  },
  methods: {
    getHospitalList() {
      this.buttonLoading = true;
      this.$api['data/hospital/hospitalList']({ ...this.queryParams, qyery: this.search }).then(res => {
        if (res.code !== 200) return;
        this.hospitalList = res.data.result;
        if (this.hospitalList.length <= 1) this.isShow = false;
      }).finally(() => (this.buttonLoading = false));
    },
    handleAdd(name, bool, row, isEdit) {
      this.title = name;
      this.visible = bool;
      if (row) this.currentRow = JSON.parse(JSON.stringify((row)));
      if (isEdit) this.isEdit = true;
    },
    handleSuccess() {
      this.loading = false;
      this.visible = false;
      this.getList();
    },
    async handleDelete(row) {
      const confirmResult = await this.$confirm('请确认是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).catch((err) => err);
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除');
      }
      this.buttonLoading = true;
      this.$api['data/goods/goodsTreeDelete']({ id: row.id }).then(res => {
        if (res.code !== 200) return;
        this.msgSuccess('删除成功');
        this.getList();
      }).finally(() => (this.buttonLoading = false));
    },
    handlePaginationChange(pagination) {
      this.queryParams = { page: pagination.pageNum, pageSize: pagination.pageSize };
      this.getList();
    },
    handleAddChild(row) {
      this.visible = true;
      // this.currentRow = row;
    },
    getList() {
      this.loading = true;
      this.$api['data/projects/pageType']({
        ...this.queryParams,
        query: { dataType: '物品类' },
      }).then(res => {
        if (res.code !== 200) return;
        this.tableData = res.data.result;
        this.total = res.data.total;
      }).finally(() => (this.loading = false));
    },
    hositalList(id) {
      console.log(id);
    },
    submitForm() {
      this.loading = true;
      this.$refs.addProduct.submitForm('ruleForm');
    },
  },
};
</script>
